package com.wbao.dianniu.im;

import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.FriendSearchAdapter;
import com.wbao.dianniu.data.AccountData;
import com.wbao.dianniu.listener.IFriendRecommendListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FriendRecommendManager;
import com.wbao.dianniu.ui.BaseFragmentActivity;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.update.ErrorDialogHelper;
import com.wbao.dianniu.utils.PermissionUtils;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class AddFriendActivity extends BaseFragmentActivity implements IFriendRecommendListener {

	private EditText mEditText;
	private ListView mListView;
	private FriendSearchAdapter adapter;
	private List<AccountData> mList;
	private LinearLayout recommendLayout;
	private RelativeLayout searchRL;
	private RelativeLayout bookRL;
	private RelativeLayout weichartRL;
	private RelativeLayout qqRL;
	private FriendRecommendManager recomManager;
	private Dialog dialog;
	private ShareSDKManager shareSDKManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_add_friends,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.add_friend_title));
		init();
		initManager();
	}

	private void initManager(){
		recomManager = new FriendRecommendManager(this);
		recomManager.addFriendRecomListener(this);
		recomManager.friendRecommendList(GlobalContext.getAccountId(), 0, 100);
		shareSDKManager = ShareSDKManager.getInstance();
	}
	
	
	private void init() {
		mEditText = (EditText) findViewById(R.id.message_search_edit);
		mListView = (ListView) findViewById(R.id.message_search_listview);
		mList = new ArrayList<>();
		adapter = new FriendSearchAdapter(this);
		adapter.setData(mList);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//这里要利用adapter.getItem(position)来获取当前position所对应的对象
				Integer accountId = ((AccountData)adapter.getItem(position)).getId();
				Utils.intoHeadInfoActivity(accountId,AddFriendActivity. this);
			}
		});
		
		recommendLayout = (LinearLayout) findViewById(R.id.recommend_layout);
		searchRL = (RelativeLayout) findViewById(R.id.rl_add_search_friend);
		bookRL = (RelativeLayout) findViewById(R.id.rl_add_book_friend);
		weichartRL = (RelativeLayout) findViewById(R.id.rl_add_weichart_friend);
		qqRL = (RelativeLayout) findViewById(R.id.rl_add_qq_friend);

		searchRL.setOnClickListener(this);
		bookRL.setOnClickListener(this);
		weichartRL.setOnClickListener(this);
		qqRL.setOnClickListener(this);
		mEditText.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.message_search_edit:
			//点击输入框，跳转到搜索界面
			Intent searchIntent = new Intent();
			searchIntent.setClass(AddFriendActivity.this, FriendSearchActivity.class);
			startActivity(searchIntent);
			break;
		case R.id.rl_add_book_friend:
			PermissionUtils.getInstance().intentToContact(AddFriendActivity.this);
			break;
		case R.id.rl_add_weichart_friend:
			if(!GlobalContext.getPerfectData()){
				ErrorDialogHelper.getInstance().showPerfectAuth(this);
				return;
			}
			// 调用分享内容
			String wc_imageUrl = GlobalContext.cdndownUrl +  GlobalContext.getHeadPic();
			String wc_content = getResources().getString(R.string.share_desc);
			shareSDKManager.showShareToWechat(this,wc_content,wc_imageUrl,GlobalContext.getAccountId());
			break;
		case R.id.rl_add_qq_friend:
			if(!GlobalContext.getPerfectData()){
				ErrorDialogHelper.getInstance().showPerfectAuth(this);
				return;
			}
			// 调用分享内容
			String qq_imageUrl = GlobalContext.cdndownUrl + GlobalContext.getHeadPic();
			String qq_content = getResources().getString(R.string.share_desc);
			shareSDKManager.showShareToQQ(this,qq_content,qq_imageUrl,GlobalContext.getAccountId());
			break;
		case R.id.rl_add_search_friend:
			if(!GlobalContext.getPerfectData()){
				ErrorDialogHelper.getInstance().showPerfectAuth(this);
				return;
			}
			// 扫一扫界面
			PermissionUtils.getInstance().intentToScan(AddFriendActivity.this);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onFriendRecomFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFriendRecomSuccess(List<AccountData> objdata) {
		// TODO Auto-generated method stub
		if(null != objdata){
			recommendLayout.setVisibility(View.VISIBLE);
			adapter.cleardata();
			mList.clear();
			mList = objdata;
			adapter.setData(mList);
			adapter.notifyDataSetChanged();
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
		}
		if(null != recomManager){
			recomManager.removeFriendRecomListener(this);
		}
	}

}
